package com.cl.mypureframe.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cl.mypureframe.R;
import com.cl.mypureframe.conn.Conn;
import com.cl.mypureframe.conn.RetrofitUtil;
import com.cl.mypureframe.minterface.OnResponseCallback;
import com.cl.mypureframe.model.MsgMod;
import com.cl.mypureframe.model.ResPageMod;
import com.cl.mypureframe.util.ImageUtils;
import com.cl.mypureframe.util.Log;
import com.cl.mypureframe.util.UtilToast;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * @Class: MyListActivity
 * @Description: 标准刷新列表布局
 * @Author: ChenLei
 * @Date: 2020/11/24---16:55
 */
public class MyListActivity extends BaseListActivity {
    private RetrofitUtil<ResPageMod> retrofitUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContenViewAndBack(R.layout.activity_list, R.string.listactivity);

        retrofitUtil = new RetrofitUtil<>();
        myAdapter = new BaseQuickAdapter<ResPageMod.PageData.PageDatas, BaseViewHolder>(R.layout.item_list) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, ResPageMod.PageData.PageDatas item) {
                baseViewHolder.setText(R.id.tv_title, item.getTitle());
                String name = TextUtils.isEmpty(item.getAuthor()) ? item.getShareUser() : item.getAuthor();
                baseViewHolder.setText(R.id.tv_name, name);
                baseViewHolder.setText(R.id.tv_fl, "| " + item.getSuperChapterName());
                baseViewHolder.setText(R.id.tv_date, "| " + item.getNiceDate());
            }
        };
        initRecyclerView();
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void onRvItemClick(BaseQuickAdapter adapter, View view, int position) {
        ResPageMod.PageData.PageDatas item = (ResPageMod.PageData.PageDatas) adapter.getItem(position);
        if (item != null) {
            startWebviewActivity(item.getTitle(), item.getLink());
        }
    }

    @Override
    public void execute() {
        Call<ResPageMod> call = retrofitUtil.getRetrofit().getPageList(currentIndex);
        retrofitUtil.connAction(call, this, new OnResponseCallback<ResPageMod>() {
            @Override
            public void onStartCallback() {
            }

            @SuppressWarnings("ConstantConditions")
            @Override
            public void onSuccessCallback(Response<ResPageMod> response) {
                ResPageMod resPageMod = response.body();
                if (resPageMod.getErrorCode() == Conn.CODE_SUCCESS) {
                    List<ResPageMod.PageData.PageDatas> pageDataList = resPageMod.getData().getDatas();
                    hasNextPage = !resPageMod.getData().isOver();
                    onSuccessAction(pageDataList);
                } else {
                    onErrorCodeAction(resPageMod.getErrorMsg());
                }
            }

            @Override
            public void onErrorCallback() {
                onErrorAction();
            }

            @Override
            public void onEndCallback() {
                onEndAction();
            }
        });
    }

}
